package my.service;

import com.anna.config.Ioc.Service;
import my.model.User;

import java.util.List;

import static my.service.UserService.LoginResult.LOGIN_OK;
import static my.service.UserService.LoginResult.PASSWORD_WRONG;
import static my.service.UserService.LoginResult.USER_NOT_EXIST;

//import my.model.User;

/**
 * Created by rajab on 17/4/13.
 */
@Service
public class UserService {
    public enum LoginResult {
        LOGIN_OK,
        INPUT_INVALID,
        PASSWORD_WRONG,
        USER_NOT_EXIST
    }

    public LoginResult checkLogin(String username, String password) {
        if (username.isEmpty() || password.isEmpty()) {
            return LoginResult.INPUT_INVALID;
        } else {
            List<User> users = User.dao.find("SELECT * FROM user where username=?", username);
            if(users.size()==0)
            {
                return USER_NOT_EXIST;
            }
            else if(users.get(0).getPassword().equals(password))
            {
                return LOGIN_OK;
            }
            else
            {
                return PASSWORD_WRONG;
            }
        }

    }

    public enum SignResult {
        INPUT_INVALID,
        USER_EXIST,
        PASSWORD_WRONG,
        SIGN_SUCCESS
    }

    public SignResult checkSign(String username, String password, String password2) {
        if (username.isEmpty() || password.isEmpty()||password2.isEmpty()) {
            return SignResult.INPUT_INVALID;
        } else if (!password.equals(password2)){
            return SignResult.PASSWORD_WRONG;
        }else {
            List<User> users = User.dao.find("SELECT * FROM user where username=?", username);
            if(users.size()!=0)
            {
                return SignResult.USER_EXIST;
            }
            else
            {
                User user=new User();
                user.setUsername(username);
                user.setPassword(password);
                user.setRole("user");
                user.save();
                return SignResult.SIGN_SUCCESS;
            }
        }
    }

    // public abstract User createUser(String username, String password, String role);
}
